﻿//-----------------------------------------------------------------------
// <copyright file="InstallationStageBase.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>20/03/2013</date>

using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.IO;
using System;
using System.Threading.Tasks;

namespace AppUpdateInstaller.Model.InstallationStages
{
	internal abstract class InstallationStageBase : IInstallationStage
	{
		public event InstallationStageProgressEventHandler ProgressChanged;

		private readonly IFileSystemContentProvider fileSystemContentProvider;
		private readonly IProcessManager processManager;
		private readonly Action shutdownAction;

		public InstallationStageBase(IProcessManager argProcessManager, IFileSystemContentProvider argFileSystemContentProvider, Action argShutdownLogic)
		{
			if (argProcessManager == null)
			{
				throw new ArgumentNullException("argProcessManager");
			}
			if (argFileSystemContentProvider == null)
			{
				throw new ArgumentNullException("argFileSystemContentProvider");
			}

			this.fileSystemContentProvider = argFileSystemContentProvider;
			this.processManager = argProcessManager;
			this.shutdownAction = argShutdownLogic;
		}

		public virtual void RollBack()
		{
			throw new NotSupportedException();
		}

		public virtual Task RollBackAsync()
		{
			throw new NotSupportedException();
		}

		public virtual void FinalizeStage()
		{
			throw new NotSupportedException();
		}

		public abstract void Run();

		public virtual Task RunAsync()
		{
			return Task.Factory.StartNew(this.Run);
		}

		protected void NotifyProgressChange(InstallationStageProgressEventArgs argEA)
		{
			InstallationStageProgressEventHandler temp = this.ProgressChanged;
			if (temp != null)
			{
				temp(this as IInstallationStage, argEA);
			}
		}

		protected IFileSystemContentProvider FileSystemContentProvider
		{
			get
			{
				return this.fileSystemContentProvider;
			}
		}

		protected IProcessManager ProcessManager
		{
			get
			{
				return this.processManager;
			}
		}

		protected Action ShutdownAction
		{
			get
			{
				return this.shutdownAction;
			}
		}

		public virtual bool SupportsRollback
		{
			get
			{
				return false;
			}
		}

		public virtual bool HasFinalizerStage
		{
			get
			{
				return false;
			}
		}
	}
}
